<template>
  <div class="tag">
    <el-tag
      class="tag-box"
      v-for="(item, index) in tags"
      :key="item.label"
      :closable="item.name !== 'home'"
      @close="handleClose(item, index)"
      @click="showInput(item)"
      :effect="$route.path == item.url ? 'dark' : 'plain'"
    >
      {{ item.label }}
    </el-tag>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
export default {
  data () {
    return {
    }
  },
  computed: {
    ...mapState({
      tags: state => state.tad.tags
    })
  },
  methods: {
    ...mapMutations(['closeTag']),
    //删除tag
    handleClose (item, index) {
      // 调用vue中的store的mutations方法
      this.closeTag(item)
      const length = this.tags.length
      //删除之后的逻辑
      if (item.name == this.$router.name) {
        return
      }

      //表识删除的最后一项
      if (index == length) {
        this.$router.push({
          name: this.tags[index - 1].name
        })
        this.$emit('EventClick', this.tags[index - 1])

      } else {
        if (this.$route.path != item.url) {
          this.$router.push({
            path: this.tags[index].url
          })
        }
        this.$emit('EventClick', this.tags[index])

      }


    },
    //点击tag跳转
    showInput (item) {
      if (this.$route.path != item.url) {
        console.log(this.$route);
        this.$router.push({ path: item.url })
        this.$emit('EventClick', item)
      }
    },
  }
}
</script>

<style lang="less" scoped>
.tag {
  margin-left: 5px;

  .tag-box {
    margin-left: 15px;
  }
}
</style>